﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CPlatePrintSys.Domain.Events;
using CPlatePrintSys.Domain.Models;
using CPlatePrintSys.Infrastructure;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Navigation.Regions;

namespace CPlatePrintSys.Win.ViewModels
{
    public class SettingsViewModel : BindableBase, INavigationAware
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;
        private User _currentUser;

        private string _subTitle = string.Empty;
        public string SubTitle
        {
            get { return _subTitle; }
            set { SetProperty(ref _subTitle, value); }
        }

        private int _curSelectedIndex = 0;
        public int CurSelectedIndex
        {
            get { return _curSelectedIndex; }
            set 
            {
                SetProperty(ref _curSelectedIndex, value);
                OnSelectionChanged(value);
            }
        }

        private object _selectedMenuItem;
        public object SelectedMenuItem
        {
            get { return _selectedMenuItem; }
            set { SetProperty(ref _selectedMenuItem, value); }
        }

        private object _selectedOptionsItem;
        public object SelectedOptionsItem
        {
            get { return _selectedOptionsItem; }
            set { SetProperty(ref _selectedOptionsItem, value); }
        }


        public ICommand BackToHomeCommand { get; }

        public SettingsViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            _subTitle = "系统设置";

            BackToHomeCommand = new DelegateCommand(BackToHome);

        }

        private void BackToHome()
        {
            _regionManager.RequestNavigate(ConstStrings.UI_Region_Secondary_Content, "HomeView");
        }


        /// <summary>
        /// 根据当前选中索引切换不同的编辑视图
        /// </summary>
        /// <param name="value"></param>
        private void OnSelectionChanged(int value)
        {
            try
            {
                System.Diagnostics.Debug.WriteLine($"OnSelectionChanged called with value: {value}");
                
                switch (value)
                {
                    case 0:
                        var parameters = new NavigationParameters();
                        parameters.Add("CurrentUser", _currentUser);
                        System.Diagnostics.Debug.WriteLine("Navigating to UserManagementView");
                        _regionManager.RequestNavigate(ConstStrings.UI_Region_Secondary_Setting_Content, "UserManagementView", parameters);
                        break;
                    case 1:
                        _regionManager.RequestNavigate(ConstStrings.UI_Region_Secondary_Setting_Content, "RoundingRuleConfigView");
                        break;
                    case 2:
                        _regionManager.RequestNavigate(ConstStrings.UI_Region_Secondary_Setting_Content, "LayerColorConfigView");
                        break;
                    case 3:
                        _regionManager.RequestNavigate(ConstStrings.UI_Region_Secondary_Setting_Content, "OutputDirectoryConfigView");
                        break;
                    case 4:
                        _regionManager.RequestNavigate(ConstStrings.UI_Region_Secondary_Setting_Content, "InterfaceConfigView");
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                // 可以添加日志记录
                System.Diagnostics.Debug.WriteLine($"导航失败: {ex.Message}");
                System.Diagnostics.Debug.WriteLine($"异常堆栈: {ex.StackTrace}");
            }
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            // 检查是否有已登录的用户（通过事件传递）
            if (navigationContext.Parameters.ContainsKey("CurrentUser"))
            {
                var user = navigationContext.Parameters["CurrentUser"] as User;
                if (user != null)
                {
                    SetCurrentUser(user);
                }
            }

            // 设置初始选择为账号管理
            CurSelectedIndex = 0;
        }

        private void SetCurrentUser(User user)
        {
           this._currentUser = user;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }
    }
}
